package com.hjm.project.statistical.snCountData.service.impl;

import com.google.common.collect.Lists;
import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.text.Convert;
import com.hjm.project.statistical.snCountData.domain.SnCountData;
import com.hjm.project.statistical.snCountData.mapper.SnCountDataMapper;
import com.hjm.project.statistical.snCountData.service.ISnCountDataService;
import com.hjm.project.system.sysConfigJob.domain.SysConfigJob;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 终端按月统计Service业务层处理
 * 
 * @author maker-platform
 * @date 2021-07-21
 */
@Service
public class SnCountDataServiceImpl implements ISnCountDataService
{
    @Resource
    private SnCountDataMapper snCountDataMapper;

    /**
     * 查询终端按月统计
     * 
     * @param id 终端按月统计ID
     * @return 终端按月统计
     */
    @Override
    public SnCountData selectSnCountDataById(Integer id)
    {
        return snCountDataMapper.selectSnCountDataById(id);
    }

    /**
     * 查询终端按月统计列表
     * 
     * @param snCountData 终端按月统计
     * @return 终端按月统计
     */
    @Override
    public List<SnCountData> selectSnCountDataList(SnCountData snCountData)
    {
        return snCountDataMapper.selectSnCountDataList(snCountData);
    }
    /**
     * 查询终端按月统计汇总金额
     *
     * @param snCountData 终端按月统计
     * @return 终端按月统计
     */
    @Override
    public SnCountData selectSnCountDataSum(SnCountData snCountData)
    {
        return snCountDataMapper.selectSnCountDataSum(snCountData);
    }

    /**
     * 查询终端按月统计列表
     *
     * @param snCountData 终端按月统计
     * @return 终端按月统计 一个结果
     */
    @Override
    public List<SnCountData> selectSnCountDataOne(SnCountData snCountData)
    {
        return snCountDataMapper.selectSnCountDataOne(snCountData);
    }

    /**
     * 新增终端按月统计
     * 
     * @param snCountData 终端按月统计
     * @return 结果
     */
    @Override
    public int insertSnCountData(SnCountData snCountData)
    {
        return snCountDataMapper.insertSnCountData(snCountData);
    }

    /**
     * 修改终端按月统计
     * 
     * @param snCountData 终端按月统计
     * @return 结果
     */
    @Override
    public int updateSnCountData(SnCountData snCountData)
    {
        return snCountDataMapper.updateSnCountData(snCountData);
    }

    /**
     * 删除终端按月统计对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSnCountDataByIds(String ids)
    {
        return snCountDataMapper.deleteSnCountDataByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除终端按月统计信息
     * 
     * @param id 终端按月统计ID
     * @return 结果
     */
    @Override
    public int deleteSnCountDataById(Integer id)
    {
        return snCountDataMapper.deleteSnCountDataById(id);
    }

    @Override
    public void suCountDataSum(SysConfigJob configJob) throws Exception {
       try{
           SnCountData snCountData = new SnCountData();
           if (configJob.getConfigId() != null) {
               snCountData.setAddDate(DateUtils.getDay(DateUtils.YYYY_MM_DD,1,configJob.getConfigValue()));
               snCountData.setLocaldate(DateUtils.getDay(DateUtils.YYYY_MM,0,configJob.getConfigValue()));
           } else {
               snCountData.setAddDate(DateUtils.getDay(DateUtils.YYYY_MM_DD,1,DateUtils.getDate()));
               snCountData.setLocaldate(DateUtils.getDateYYYY_MM());
           }
           snCountDataMapper.deleteSnCountData(snCountData.getLocaldate());
           List<SnCountData> list = snCountDataMapper.suCountDataSum(snCountData);
           if (CollectionUtils.isNotEmpty(list)) {
               List<List<SnCountData>> partition = Lists.partition(list, 500);
               for (List<SnCountData> countData : partition) {
                   int i = snCountDataMapper.insertSnCountDataBatch(countData);
                   if (i <= 0) {
                       throw new BusinessException("统计结果新增失败,更新结果:" + i);
                   }
               }
           } else {
               throw new BusinessException("统计结果集为空:" + list.size());
           }
       }catch (Exception e){
            throw new BusinessException("统计异常：" + StringUtils.substring(e.getMessage(), 0, 2000));
       }
    }

}
